package com.lianxi.Courses.controller;

import com.github.pagehelper.PageInfo;
import com.lianxi.Courses.constant.ResponseCodeConstant;
import com.lianxi.Courses.dao.UserMapper;
import com.lianxi.Courses.entity.Resource;
import com.lianxi.Courses.entity.User;
import com.lianxi.Courses.entity.UserResource;
import com.lianxi.Courses.exception.*;
import com.lianxi.Courses.service.UserResourceService;
import com.lianxi.Courses.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * 翔哥
 * 2025年10月14日
 */
@RestController
@RequestMapping("/userResource")
public class UserResourceController {

    @Autowired
    private UserResourceService userResourceService;
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/findAll")
    public ModelAndView findAll(String pageNo, String pageSize,String choose ,HttpSession session) {
        ModelAndView mav = new ModelAndView();
        User user = (User) session.getAttribute("user");
        String userId = String.valueOf(user.getId());

        PageInfo<UserResource> userRePage = userResourceService.findPage(userId,pageNo,pageSize);
        mav.addObject("userRePage",userRePage);
        PageInfo<Resource> userRePage02 = userResourceService.findMy(userId,choose != null && choose.equals("resource") ? pageNo : "1", pageSize);
        mav.addObject("userRePage02",userRePage02);
        mav.addObject("choose", choose != null ? choose : "study");

        System.out.println();
        mav.setViewName("backend/front_mycourse");

        return mav;
    }
    @RequestMapping("/findMy")
    public ModelAndView findMy(String pageNo, String pageSize,String choose, HttpSession session) {
        ModelAndView mav = new ModelAndView();
        User user = (User) session.getAttribute("user");
        String userId = String.valueOf(user.getId());

        PageInfo<UserResource> userRePage = userResourceService.findPage(userId,choose != null && choose.equals("resource") ? pageNo : "1", pageSize);
        mav.addObject("userRePage",userRePage);
        PageInfo<Resource> userRePage02 = userResourceService.findMy(userId,pageNo,pageSize);
        mav.addObject("userRePage02",userRePage02);
        mav.addObject("choose", choose != null ? choose : "resource");
        mav.setViewName("backend/front_mycourse");

        return mav;
    }

    @RequestMapping("/findById")
    public ResponseResult findById(String id) {
        ResponseResult result =new ResponseResult();
        try {
            Resource resource =userResourceService.findById(id);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
            result.setReturnObject(resource);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @RequestMapping("/updateByRe")
    public ResponseResult updateByRe(String id,String title,@RequestParam("file") CommonsMultipartFile file , String costType, String costNumber, HttpSession session){
        ResponseResult result = new ResponseResult();

        try {
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
            userResourceService.updateByRe(id, title,file, costType, costNumber, session);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (FileUploadErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (ResourceErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        }

        catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        return result;
    }

    @RequestMapping("/addByRe")
    public ResponseResult addByRe(String title ,@RequestParam("file") CommonsMultipartFile file , String costType, String costNumber, HttpSession session) {
        ResponseResult result = new ResponseResult();

        try {
            userResourceService.addByRe(title, file, costType, costNumber, session);
            result.setMessage("添加资源成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (FileUploadErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (ResourceErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常: " + e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @RequestMapping("/deleteByRe")
    public ResponseResult deleteByRe(String id, HttpSession session){
        ResponseResult result = new ResponseResult();
        try {
            userResourceService.deleteByRe(id, session);
            result.setMessage("添加资源删除");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        }
        return result;
    }

}
